import React from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { Link } from 'react-router';
import Switch from 'rc-switch';

import {
  ListGroup,
  ListGroupItem
} from 'react-bootstrap';

import * as Shell from '../common/Shell';
import Notify from '../common/Notification';
import { recommend as RecommendConfig } from '../config/Common';
import * as RecommendActions from '../actions/RecommendActions';
import Container from '../components/Container';
import Copyright from '../components/Copyright';
import ListItem from '../components/ListItem';
import DialogCommon from '../components/DialogCommon';
import DialogAlert from '../components/dialog/DialogAlert';
import * as UserActions from '../actions/UserActions';
import * as LoginActions from '../actions/LoginActions';
import * as ProductActions from '../actions/ProductActions';

class SettingApp extends React.Component {

  constructor () {
    super(); 

    this.state = {
      dialogVisiable : false,
      dialogTitle : "",
      dialogContent : <div>系统提示</div>,
      dialogButton : null,
      recommendStatus : false
    };
  }

  static propTypes = {
    
  }

  componentDidMount(){
    let { user } = this.props;
    let recommendStatus = false;
    if (user.isrecommend && user.isrecommend === "1") {
      recommendStatus = true;
    }

    this.setState({
      recommendStatus: recommendStatus
    });
  }

  onRecommendChangeHandle(value){
    let recommendStatus = false;
    //订阅
    if (value === true) {
      let opStatus = RecommendConfig.recommendStatus.hasSign;
      this.props.recommendAction.signRecommend(opStatus);
    }
    //取消订阅
    else if(value === false){
      let opStatus = RecommendConfig.recommendStatus.noSign;
      this.props.recommendAction.unsignRecommend(opStatus);
    }

    this.setState({
      recommendStatus: value
    });
  }

  componentDidUpdate(){
    let { recommend_sign_status, recommend_unsign_status } = this.props.recommend;
    if (recommend_sign_status===1 || recommend_unsign_status===1) {
       //this.props.userAction.fetchUser();
    }

    this.props.recommend.recommend_sign_status = 0;
    this.props.recommend.recommend_unsign_status = 0;

    let { logout_status, message } = this.props.login;
    if (logout_status === -1) {
      Notify.makeNotify(message);
    }else if(logout_status === 1){
      
      //清除壳子保存的用户凭据信息
      Shell.callShellMethod(
        "logoutPlugin"
      );

      //复位产品信息
      this.props.productAction.resetProduct();
      //复位用户信息
      this.props.userAction.resetUser();
      //km：消息/记录，暂不复位

      this.props.history.pushState(null, "/Account");
    }

    this.props.login.logout_status = 0;
  }

  handleLogout(){
    let { loginAction } = this.props;
    loginAction.logout();
  }

  getSettingHeader(title){
      return (
        <a>
          <div className="list-head">
            <h4 className="list-title text-info">{title}</h4>
          </div>
        </a>
      );
  }

  onLogoutClick(){
    this.setState({
      dialogContent : <DialogAlert message={"确定要退出股票专家吗？"}/>,
      dialogVisiable : true,
      dialogTitle : "温馨提示",
      dialogButton : [{
         text : "确定",
         style : "primary",
         callback : function(){
            this.handleLogout();
            this.handleDialogHide();
         }.bind(this)
      },{
        text : "取消",
        style :"default"
      }]
    });
  }

  handleDialogHide(){
    this.setState({
       dialogVisiable : false
    });
  }

  render() {

    let copyRight = 
    <Copyright >
      <a className="border-bottom text-primary" onClick={this.onLogoutClick.bind(this)}>
        退出登录
      </a>
    </Copyright>;

    return (
          <Container 
              isGoBack={true} 
              isHeader={true} 
              isFooter={false} 
              copyRight={copyRight}
              headerTitle={"设置"}>

              <div className="blank20"></div>
              <ListGroup>
                  <ListGroupItem className="list-item"  header={ this.getSettingHeader("免费盘前股讯服务") }>

                      <div className="list-extra list-extra-right"> 

                        <Switch 
                          disabled={false}
                          checked={ this.state.recommendStatus }
                          onChange = { this.onRecommendChangeHandle.bind(this) }
                          checkedChildren={'已订阅'}
                          unCheckedChildren={'未订阅'} />

                      </div>  
                    
                  </ListGroupItem>
              </ListGroup>

              <DialogCommon 
                  title={ this.state.dialogTitle } 
                  content={ this.state.dialogContent } 
                  buttons = { this.state.dialogButton }
                  visiable={ this.state.dialogVisiable } 
                  onHide = { this.handleDialogHide.bind(this) }/>
          </Container>
    );
  }
}

export default connect(state => ({
    recommend: state.recommend,
    user : state.user,
    login: state.login
}), dispatch => ({
    recommendAction: bindActionCreators(RecommendActions, dispatch),
    loginAction: bindActionCreators(LoginActions, dispatch),
    userAction : bindActionCreators(UserActions, dispatch),
    productAction : bindActionCreators(ProductActions, dispatch)
}))(SettingApp);
